//
//  BillboardListViewController.m
//  bitcoin
//
//  Created by  旺链 on 2018/6/12.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "BillboardListViewController.h"
#import "BillboardTableViewCell.h"
#import "HooDatePicker.h"

@interface BillboardListViewController ()<UITableViewDelegate,UITableViewDataSource,HooDatePickerDelegate>

@property(nonatomic,assign)NSInteger pageNum;
@property(nonatomic,assign)NSInteger pageSize;
@property(nonatomic,strong)NSString *billboardMonth;

@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UITableView *table;

@property(nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic, strong) HooDatePicker *datePicker;
@end

@implementation BillboardListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"榜单列表";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#1C2035"];
    
    self.pageNum = 1;
    self.pageSize = 10;
    
    NSDateFormatter *dateFormatter = [NSDate shareDateFormatter];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    self.billboardMonth = [dateFormatter stringFromDate:[NSDate date]];
    
    [self setUI];
    [self getData];
}
- (void)getData
{
    NSDictionary *para= @{@"pageNum":@(self.pageNum),@"pageSize":@(self.pageSize),@"billboardMonth":self.billboardMonth};
    [LSRequestManager postRequestWithURL:MARKET(@"getCommissionDiscountBillboard") params:para success:^(NSDictionary *responseData) {
        
        HandleResponse(
                       [self.dataArr addObjectsFromArray:responseData[@"data"]];
                       [self.table reloadData];
                       )}
        
        
    } failure:^(NSError *error) {
        
    }];
}
- (void)setUI
{
    self.btn = [[UIButton alloc]init];
    [self.btn setImage:[UIImage imageNamed:@"date-icon"] forState:UIControlStateNormal];
    [self.btn setTitle:[NSString stringWithFormat:@"  %@",self.billboardMonth] forState:UIControlStateNormal];
    [self.btn setTintColor:[UIColor colorWithHexString:@"#7B4EE1"]];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.btn addTarget:self action:@selector(chooseDate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(15);
        make.height.equalTo(45);
    }];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.btn.mm_bottom, ScreenWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"#2E3145"];
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.btn.bottom);
        make.left.right.equalTo(0);
        make.height.equalTo(0.5);
    }];
    
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(line.bottom);
        make.left.right.bottom.equalTo(0);
    }];
    
    [self.view addSubview:self.datePicker];
}
- (void)chooseDate
{
    [self.datePicker show];
    
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (HooDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[HooDatePicker alloc] initWithSuperView:self.view];
        _datePicker.datePickerMode = HooDatePickerModeYearAndMonth;
        _datePicker.delegate = self;
        
        NSDateFormatter *dateFormatter = [NSDate shareDateFormatter];
        [dateFormatter setDateFormat:kDateFormatYYYYMMDD];
        NSDate *maxDate = [dateFormatter dateFromString:@"2050-01-01"];
        NSDate *minDate = [dateFormatter dateFromString:@"2016-01-01"];
        
        [_datePicker setDate:[NSDate date] animated:YES];
        _datePicker.minimumDate = minDate;
        _datePicker.maximumDate = maxDate;
    }
    return _datePicker;
}
- (UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, self.btn.mm_bottom+1, ScreenWidth, ScreenHeight-self.btn.mm_bottom-0.5-(NavgationHeight)) style:UITableViewStylePlain];
        _table.backgroundColor = [UIColor colorWithHexString:@"#1C2035"];
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.delegate = self;
        _table.dataSource = self;
        
        [_table registerNib:[UINib nibWithNibName:@"BillboardTableViewCell" bundle:nil] forCellReuseIdentifier:@"BillboardTableViewCell"];
        
    }
    return _table;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BillboardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BillboardTableViewCell"];
    cell.num.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    
    NSDictionary *dic = self.dataArr[indexPath.row];
    
    cell.acountLb.text = dic[@"userAccount"];
    cell.moneyLb.text = [NSString stringWithFormat:@"%@%@",dic[@"commissionDiscountAmount"],dic[@"commissionDiscountUnit"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (void)datePicker:(HooDatePicker *)datePicker dateDidChange:(NSDate *)date
{
    
}
- (void)datePicker:(HooDatePicker *)datePicker didCancel:(UIButton *)sender
{
    
}

- (void)datePicker:(HooDatePicker *)dataPicker didSelectedDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [NSDate shareDateFormatter];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    self.billboardMonth = [dateFormatter stringFromDate:date];
    [self.btn setTitle:[NSString stringWithFormat:@"  %@",self.billboardMonth] forState:UIControlStateNormal];
    [self getData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
